﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Model;
using DAL;
using IDAL;


namespace ClientUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load_1(object sender, EventArgs e)
        {
            //查询所有用户
            var data = dal.Inquire();
            //将数据绑定到DataGridView
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = data;
        }


        DAL.DALUser dal = new DAL.DALUser();

        #region 添加用户
        private void butAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text) || string.IsNullOrEmpty(textBox3.Text))
            {
                MessageBox.Show("请检查ID,Name,Email");
                return;
            }
            var data = dal.Add(new User(int.Parse(textBox2.Text), textBox1.Text, textBox3.Text));
            if (data!=null)
            {
                //将数据绑定到DataGridView
                dataGridView1.DataSource = null;
                dataGridView1.DataSource = data;
                MessageBox.Show("添加成功");
                return;
            }
            else
            {
                MessageBox.Show("添加失败");
                return;
            }
        }
        #endregion

        #region 删除用户
        private void butDEL_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("请检查ID");
                return;
            }
            var data = dal.Delete( int.Parse(textBox2.Text));
            if (data != null)
            {
                //将数据绑定到DataGridView
                dataGridView1.DataSource = null;
                dataGridView1.DataSource = data;
                MessageBox.Show("删除成功");
                return;
            }
            else
            {
                MessageBox.Show("删除失败");
                return;
            }
        }
        #endregion
      
        #region  改
        private void butUPDATE_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text) || string.IsNullOrEmpty(textBox3.Text))
            {
                MessageBox.Show("请检查ID,Name,Email");
                return;
            }
            var data = dal.Update(new User(int.Parse(textBox2.Text), textBox1.Text, textBox3.Text));
            if (data != null)
            {
                //将数据绑定到DataGridView
                dataGridView1.DataSource = null;
                dataGridView1.DataSource = data;
                MessageBox.Show("修改成功");
                return;
            }
            else
            {
                MessageBox.Show("修改失败");
                return;
            }
        }
        #endregion

        #region 查
        private void butGET_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("请检查ID");
                return;
            }
            var data = dal.Inquire(int.Parse(textBox2.Text));
            if (data != null)
            {
                //将数据绑定到DataGridView
                dataGridView1.DataSource = null;
                dataGridView1.DataSource = data;
                MessageBox.Show("查询成功");
                return;
            }
            else
            {
                MessageBox.Show("查询失败");
                return;
            }
        }
        #endregion
    }
}
